﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mendel
{
    public class List : Data
    {
        public List<Item> Value { get; private set; }

        public List(IEnumerable<Item> v)
        {
            Value = new List<Item>(v);
        }

        public override DType Type
        {
            get { return DType.List; }
        }

        public override Data Clone()
        {
            return new List(Value);
        }

        public override Data Convert(DType to)
        {
            switch (to)
            {
                case DType.Integer:
                    return new Integer((int)Value.Where(p => p.Type == DType.Integer).Sum(p => p.IN));
                case DType.Float:
                    return new Float((double)Value.Where(p => p.Type == DType.Float || p.Type==DType.Integer).Sum(p => p.FL));
                case DType.String:
                    string ret="";
                    foreach (Item item in Value)
                    {
                        if (item.Constant)
                            ret += item.ST + " ";
                        else
                            ret += item.Name + " ";
                    }
                    return new String(ret);
                case DType.List:
                    return this;
                case DType.Word:
                    return new Word(Value);
                case DType.Reference:
                    return new Reference(Container);
                default:
                    return this;
            }
        }
    }
}
